﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;

namespace Licesing
{
    public class MacAdress : PhysicalAddress
    {
        private const int MacTypeIndex = 1;
        private const int NicTypeIndex = 0;

        public MacAdress(byte[] address):
            base(address){}

        public AdministerType MacType{
            get{
                return (AdministerType) GetBitValue(GetAddressBytes()[0], MacTypeIndex);
            }
        }

        public DeviceType NicType{
            get{
                return (DeviceType)GetBitValue(GetAddressBytes()[0], NicTypeIndex);
            }
        }

        public String MacTypeString
        {
            get
            {
                if (MacType == AdministerType.Global)
                    return "Global";
                return "Local";
            }
        }

        public String NicTypeString
        {
            get
            {
                if (NicType == DeviceType.Unicast)
                    return "Unicast";
                return "Multicast";
            }
        }

        private static int GetBitValue(byte b,int i)
        {
            var bitarray = new BitArray(new byte[] { b });
            var bit = bitarray.Get(i);
            return bit ? 1 : 0;
        }

        public override string ToString()
        {
            return String.Format("Physical adress:{0}{3}" +
                                 "{4}Communicator type:{1}{3} " +
                                 "{4}Administrative Type:{2}{3}", 
                                 base.ToString(),NicTypeString,MacTypeString,
                                 Environment.NewLine,"\t");
        }
    }
    //TODO: May be not to use integers. Use Boolean, as bits presented in bytes. Lol.
        /// <summary>
        /// Mac address least significant bit of the most significant byte. Represents a networc device type.
        /// </summary>
        public enum DeviceType
        {
            /// <summary>
            /// The frame is meant to reach a group of receiving Network interface controller within the collision domain.
            /// </summary>
            Multicast = 1,
            /// <summary>
            /// The frame is meant to reach only one receiving Network interface controller.
            /// </summary>
            Unicast = 0,
        }
        
        /// <summary>
        /// Mac address second least significant bit of the most significant byte. Represents a networc device type
        /// </summary>
        public enum AdministerType
        {
            /// <summary>
            /// MAC address is universally administered.
            /// </summary>
            Global = 0,
            /// <summary>
            /// MAC address is locally administered.
            /// </summary>
            Local = 1
        }
}
